<template>
  <div class="shouye-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar">
      <van-button
        slot="title"
        type="info"
        size="small"
        class="search-btn"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <van-tabs v-model="active" swipeable class="channels-tabs">
      <van-tab :title="obj.name" v-for="obj in channels" :key="obj.id">
        <Articlelist :channel="obj"></Articlelist>
      </van-tab>
      <div class="placeholder" slot="nav-right"></div>
      <div
        class="hamburger-btn"
        slot="nav-right"
        @click="isChannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!--  -->
    <!-- <van-tabs class="channel-tabs" v-model="active" swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        
        <article-list ref="article-list" :channel="channel" />
    
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChennelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs> -->
    <!-- 频道编辑 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <channeledit
        :myChannel="channels"
        :active="active"
        @update-active="updataactive2"
      ></channeledit>
    </van-popup>
  </div>
</template>

<script>
import { getChannels } from "@/api/user";
// import ArticleList from "@/views/home/Articlelist.vue";
import channeledit from "@/views/home/component/channel-edit.vue";
import Articlelist from "@/views/home/component/Articlelist.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
export default {
  data() {
    return {
      active: 2,
      channels:[], //频道列表
      isChannelEditShow: true,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  components: {
    Articlelist,
    // ArticleList,
    channeledit,
  },
  created() {
    this.loadChannels();
  },
  methods: {
    updataactive2(index, isChannelEditShow) {
      this.active = index;
      this.isChannelEditShow = isChannelEditShow;
    },
    async loadChannels() {
      // try {
      //   const { data } = await getChannels();
      //   this.channels = data.data.channels;
      // } catch (err) {
      //   this.$toast("频道获取失败");
      // }
      try {
        let channels = [];
        if (this.user) {
          //已登录，请求获取线上的频道数据
          const { data } = await getChannels();
          channels = data.data.channels;
        } else {
          // 未登录
          const localChannels = getItem("TOUTIAO-CHANNEL");
          if (localChannels) {
            // 如果本地有数据，则使用
            channels = localChannels;
          } else {
            // 如果没有数据，则请求获取推荐的频道列表数据
            const { data } = await getChannels();
            channels = data.data.channels;
          }
        }
        // 最后将数据更新到组件中
        this.channels=channels;
        this.$toast('数据获取成功')
      } catch (err) {
        console.log(err);
        this.$toast("数据获取失败");
      }
    },
  }
}
</script>

<style lang="less" scoped>
.shouye-container {
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .channels-tabs {
    min-width: 200px;
    height: 82px;
    border-right: 1px solid #edeff3;
    font-size: 30px;
    color: #777;
  }
  .van-tab--active {
    color: #333;
  }
  /deep/ .van-tabs__nav {
    padding-bottom: 0;
  }
  /deep/.van-tabs__line {
    width: 31px !important;
    height: 6px;
    background-color: #3296fa;
    bottom: 8px;
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
